﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Practices.Unity;
using Autocentro.BussinesLogic;
using Autocentro.Entities;

namespace Autocentro.UI
{
    public partial class FrmCompra : Form
    {
        [Dependency]
        public ICompraBL CompraBL { get; set; }

        [Dependency]
        public IDetalleCompraBL detCompraBL { get; set; }

        public FrmCompra()
        {
            InitializeComponent();
            this.BuildUp();
        }

        private void FrmCompra_Load(object sender, EventArgs e)
        {
            dateTimePicker1.Enabled = false;
            dateTimePicker2.Enabled = false;
            lblFec1.Text = "Desde:";
            ReiniciarFechas();
            
            CargaData();
        }

        private void txtCriterio_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                CargaData();
            }
        }

        private void ReiniciarFechas()
        {
            dateTimePicker1.Value = DateTime.Now.Date;
            var fec2 = dateTimePicker1.Value.Date;
            dateTimePicker2.Value = fec2.AddDays(1);
        }

        private void CargaData()
        {
            if (rbtnGeneral.Checked) // solo cliente
            {
                dateTimePicker1.Enabled = false;
                dateTimePicker2.Enabled = false;
                dateTimePicker2.Visible = true;
                lblFec2.Visible = true;
                lblFec1.Text = "Desde:";

                cOMPRASBindingSource.DataSource = CompraBL.GetFromComprasbyFechasAndProveedor(null, null, txtCriterio.Text);
            }

            if (rbtnFechas.Checked == true) // cliente y fechas
            {
                dateTimePicker1.Enabled = true;
                dateTimePicker2.Enabled = true;
                dateTimePicker2.Visible = true;
                lblFec1.Text = "Desde:";
                lblFec2.Visible = true;

                var fec1 = dateTimePicker1.Value.Date;
                var fec2 = dateTimePicker2.Value.Date;

                if (fec1 <= fec2)
                {
                    cOMPRASBindingSource.DataSource = CompraBL.GetFromComprasbyFechasAndProveedor(fec1, fec2, txtCriterio.Text);
                }
                else
                {
                    ReiniciarFechas();
                    MessageBox.Show("La fecha de inicio debe ser menor o igual a la fecha de término", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            if (rbtnDia.Checked == true) // cliente y dia 
            {
                dateTimePicker1.Enabled = true;
                dateTimePicker2.Visible = false;
                lblFec2.Visible = false;
                lblFec1.Text = "Día:";

                var fec1 = dateTimePicker1.Value.Date;

                cOMPRASBindingSource.DataSource = CompraBL.GetFromComprasbyFechasAndProveedor(fec1, null, txtCriterio.Text);
            }


            var _currentCompra = cOMPRASBindingSource.Current as COMPRAS;
            {
                if (_currentCompra != null)
                {
                    detalleCompraBindingSource.DataSource = detCompraBL.GetFromDetalleCompraByProductoDescAndCompraId("", _currentCompra.Id);
                }
                else
                {
                    detalleCompraBindingSource.Clear();
                }
            }

            //if (txtCriterio.Text.Length > 0)
            //{
                //var criterio = txtCriterio.Text == string.Empty ? null : txtCriterio.Text;
                //cOMPRASBindingSource.DataSource = CompraBL.GetAllFromComprasByCriterio(criterio);
            //}
            //else else MessageBox.Show("Es necesario ingresar almenos un caracter", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            CargaData();
        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            var frmCompra = new FrmMttCompra();
            frmCompra.AddCompra();
            frmCompra.ShowDialog();
            CargaData();
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            var compra = cOMPRASBindingSource.Current as COMPRAS;
            if (compra!=null)
            {
                var frmCompra = new FrmMttCompra();
                frmCompra.CompraUpdate(compra.Id);
                frmCompra.ShowDialog();
                CargaData();
            }
            else
            {
                MessageBox.Show("Debe de seleccionar una compra");
            }
            
        }

        private void cOMPRASDataGridView_CurrentCellChanged(object sender, EventArgs e)
        {
            COMPRAS currentcompra = cOMPRASBindingSource.Current as COMPRAS;
            if (currentcompra != null)
            {
                detalleCompraBindingSource.DataSource = detCompraBL.GetAllFromDetalleCompraByCompraId(currentcompra.Id);
            }
        }

        private void TxtProductoDesc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                BuscarEnDetalleCompra();
            }
        }

        private void BtnBuscarProducto_Click(object sender, EventArgs e)
        {
            BuscarEnDetalleCompra();
        }

        private void BuscarEnDetalleCompra()
        {
            COMPRAS _currentCompra = cOMPRASBindingSource.Current as COMPRAS;

            if (_currentCompra != null)
            {
                detalleCompraBindingSource.DataSource = detCompraBL.GetFromDetalleCompraByProductoDescAndCompraId(TxtProductoDesc.Text, _currentCompra.Id);
            }
            else
            {
                MessageBox.Show("Debe seleccionar una Compra", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void rbtnGeneral_Click(object sender, EventArgs e)
        {
            CargaData();
        }

        private void rbtnFechas_Click(object sender, EventArgs e)
        {
            CargaData();
        }

        private void rbtnDia_Click(object sender, EventArgs e)
        {
            CargaData();
        }

        private void txtCriterio_KeyPress(object sender, KeyPressEventArgs e)
        {
            var slash = Convert.ToString("/");
            var coma = Convert.ToString(",");
            var guion = Convert.ToString("-");
            var punto = Convert.ToString(".");
            var amberson = Convert.ToString("&");

            string cadena = slash + coma + guion + punto + amberson + (char)8;
            if (Char.IsLetter(e.KeyChar) || cadena.Contains(e.KeyChar) || (char.IsNumber(e.KeyChar)))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void TxtProductoDesc_KeyPress(object sender, KeyPressEventArgs e)
        {
            var slash = Convert.ToString("/");
            var coma = Convert.ToString(",");
            var guion = Convert.ToString("-");
            var punto = Convert.ToString(".");

            string cadena = slash + coma + guion + punto + (char)8;

            if (Char.IsLetter(e.KeyChar) || cadena.Contains(e.KeyChar) || (char.IsNumber(e.KeyChar)))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        
    }
}
